package co.wangdao.xingshaoye.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

import co.wangdao.xingshaoye.MainActivity;
import co.wangdao.xingshaoye.R;
import co.wangdao.xingshaoye.utils.Utils;
import co.wangdao.xingshaoye.view.fragment.Fragment_main;

/**
 * Created by Administrator on 2016/3/1 0001.
 */
public class LocationActivity extends Activity implements OnClickListener,AMapLocationListener {

    private LinearLayout addnewaddress;
    private LinearLayout dangqianweizhi;
    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient;
    private AMapLocationClientOption locationOption ;
    private String positonString;
    private TextView weizhitext;//定位的位置信息存放位置
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_location);
        initView();




        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        locationOption = new AMapLocationClientOption();
        // 设置定位模式为高精度模式
        locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        locationOption.setInterval(1000*60*10);
        //设置定位回调监听
        mLocationClient.setLocationListener(this);
        mLocationClient.setLocationOption(locationOption);










        addnewaddress.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LocationActivity.this,AddaddressActivity.class);
                startActivity(intent);
            }
        });
        dangqianweizhi.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取当前位置更新到主页面

                mLocationClient.startLocation();


            }
        });




    }


    private void initView(){
        addnewaddress = (LinearLayout) findViewById(R.id.addnewaddress);
        dangqianweizhi = (LinearLayout) findViewById(R.id.dangqianweizhi);
        weizhitext = (TextView) findViewById(R.id.weizhitext);
    }


    @Override
    public void onClick(View v) {

    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {

        positonString = aMapLocation.getAddress();

        mHandler.sendEmptyMessage(Utils.MSG_LOCATION_FINISH);
    }


    Handler mHandler = new Handler() {
        public void dispatchMessage(android.os.Message msg) {
            switch (msg.what) {
                //开始定位
                case Utils.MSG_LOCATION_START:

                    break;
                // 定位完成
                case Utils.MSG_LOCATION_FINISH:
//                    AMapLocation loc = (AMapLocation) msg.obj;
//                    String result = Utils.getLocationStr(loc);
                    weizhitext.setText(positonString);
                    Intent intent = new Intent();
                    intent.putExtra("positonString",positonString);
                    setResult(Activity.RESULT_OK,intent);
                    finish();
                    break;
                //停止定位
                case Utils.MSG_LOCATION_STOP:
                    //tvReult.setText("定位停止");
                    break;
                default:
                    break;
            }
        };
    };

}
